//
//  TheNewMVViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "TheNewMVViewController.h"
#import "ClassifyButtonView.h"
#import "EveryDaySendCollectionViewCell.h"
#import "MVClassifyCollectionViewCell.h"
#import "MVAllKindsClassifyViewController.h"
#import "MVTypeModel.h"
#import "AVPlayerViewController.h"
#import "MvListModel.h"
@interface TheNewMVViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,  classifyButtonViewDelegate, MVClassifyCollectionViewCellDelegate, EveryDaySendCollectionViewCellDelegate>
@property(nonatomic, retain) ClassifyButtonView *classifyButtonView;
@property(nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, retain) MVClassifyCollectionViewCell *mvCell;
@end

@implementation TheNewMVViewController
- (void)dealloc {
    [_titleString release];
    [_classifyButtonView release];
    [_collectionView release];
    [_mvCell release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建ClassifyButtonView
    [self createClassifyButtonView];
    //创建CollectionView
    [self createCollectionView];
}
- (void)viewWillAppear:(BOOL)animated {
    MVTypeModel *model = [MVTypeModel shareMVTypeModel];
    if (model.ID == nil) {
        _mvCell.dataArray = [NSMutableArray array];
        [_mvCell loadData:@"2" idString:@"0"];
    }else {
        _mvCell.dataArray = [NSMutableArray array];
        [_mvCell loadData:@"2" idString:model.ID];
    }
    [_mvCell.header.HotButton setTitleColor:[UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    [_mvCell.header.NewButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
#pragma mark 创建ClassifyButtonView
- (void)createClassifyButtonView {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.title = @"MV";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.classifyButtonView = [[ClassifyButtonView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    _classifyButtonView.titleArray = [NSMutableArray arrayWithArray:@[@"每日一发", @"MV分类"]];
    _classifyButtonView.backView.backgroundColor = [UIColor colorWithRed:0.094 green:0.145 blue:0.200 alpha:1.000];
    _classifyButtonView.showLength = YES;
    _classifyButtonView.delegate = self;
    [self.view addSubview:_classifyButtonView];
    [_classifyButtonView release];
}
- (void) leftBarButtonItemAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 创建collectionView
- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 100) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.backgroundColor = [UIColor clearColor];
    //注册第一个cell的重用标识
    [_collectionView registerClass:[EveryDaySendCollectionViewCell class] forCellWithReuseIdentifier:@"EveryDaySendCollectionViewCellIdentify"];
    //注册第二个cell的重用标识
    [_collectionView registerClass:[MVClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"MVClassifyCollectionViewCellIdentify"];
    [self.view addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        EveryDaySendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EveryDaySendCollectionViewCellIdentify" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.delegate = self;
        return cell;
    }else {
        self.mvCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MVClassifyCollectionViewCellIdentify" forIndexPath:indexPath];
        _mvCell.delegate = self;
        return _mvCell;
    }
}
#pragma mark cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
#pragma mark cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)rcollectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width, self.collectionView.frame.size.height );
}
#pragma mark 最小cell间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
#pragma mark 最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
#pragma mark 头视图中全部button的点击事件
- (void)allButtonClickedAction:(NSString *)title {
    MVAllKindsClassifyViewController *view = [[MVAllKindsClassifyViewController alloc]init];
    view.titleString = title;
    [self presentViewController:view animated:YES completion:^{
    }];
    [view release];
}
#pragma mark 播放的点击事件
- (void)showButtonClickAction:(NSMutableArray *)mvListArray {
    AVPlayerViewController *player = [AVPlayerViewController new];
    player.urlString = [mvListArray.lastObject urlStr];
    player.idString = [mvListArray.lastObject videoId];
    BaseClassNavigationController *navgationController = [[BaseClassNavigationController alloc] initWithRootViewController:player];
    [navgationController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsCompact];
    [self presentViewController:navgationController animated:YES completion:^{
        
    }];
    [navgationController release];
    [player release];

     NSLog(@"播放");
}
- (void)everyItemClickedAction:(NSMutableArray *)mvListArray {
    AVPlayerViewController *player = [AVPlayerViewController new];
    BaseClassNavigationController *navgationController = [[BaseClassNavigationController alloc] initWithRootViewController:player];
    player.urlString = [mvListArray.lastObject urlStr];
    player.idString = [mvListArray.lastObject videoId];
    [navgationController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsCompact];
    [self presentViewController:navgationController animated:YES completion:^{
        
    }];
    [navgationController release];
    [player release];
}
#pragma mark 分类button的点击事件
-(void)buttonClickAction:(UIButton *)button {
    _collectionView.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 2500), 0);
}
#pragma mark collectionView的滑动事件
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == _collectionView) {
        _classifyButtonView.buttonTag = 2500 +_collectionView.contentOffset.x / self.view.frame.size.width;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
